﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using TextAnalytics;
using TextAnalytics.Operations;
using TextAnalytics.Models;

namespace TextAnalyticsTests
{
    [TestClass]
    public class ProjectOperationsTests
    {
        public string ProjectRootPath;

        [TestInitializeAttribute]
        public void SetUp()
        {
            ProjectRootPath = Path.Combine(Path.GetTempPath(), "PS");
            if (Directory.Exists(ProjectRootPath))
                Directory.Delete(ProjectRootPath, true);
            Directory.CreateDirectory(ProjectRootPath);
        }

        [TestMethod]
        public void PS_can_create_project()
        {
            var projectModel = ProjectOperations.CreateNewProject("TestProject1", ProjectRootPath);

            Assert.IsTrue(Directory.Exists(projectModel.RootDirectoryPath), "Project folder doesn't exist!");
            Assert.IsTrue(Directory.Exists(projectModel.DataDirectoryPath), "Data folder doesn't exist!");
            Assert.IsTrue(File.Exists(projectModel.GetDbPath()), "Local DB doesn't exist!");
        }

        [TestMethod]
        public void PS_throws_exception_if_project_already_exists()
        {
            var model = ProjectOperations.CreateNewProject("TestProject2", ProjectRootPath);
            bool exceptionWasThrown = false;
            try
            {
                model = ProjectOperations.CreateNewProject("TestProject2", ProjectRootPath);
            }
            catch (ProjectAlreadyExistsException)
            {
                exceptionWasThrown = true;
            }

            Assert.IsTrue(exceptionWasThrown, "The project was overwritten!!!!");
        }

        [TestMethod]
        public void PS_can_open_existing_project()
        {
            var newProjectModel = ProjectOperations.CreateNewProject("TestProject3", ProjectRootPath);

            var existingProjectModel = ProjectOperations.OpenExistingProject(newProjectModel.GetProjectPath());

            Assert.AreEqual(newProjectModel.GetProjectPath(), existingProjectModel.GetProjectPath(), "The newly created differes after reopening. Could be Serialization issue.");
            Assert.AreEqual(newProjectModel.GetDbPath(), existingProjectModel.GetDbPath(), "The newly created differes after reopening. Could be Serialization issue.");
            Assert.AreEqual(newProjectModel.RootDirectory.DirectoryPath, existingProjectModel.RootDirectory.DirectoryPath, "The newly created differes after reopening. Could be Serialization issue.");
        }

        //[TestMethod]
        //public void PS_target_directory_must_be_empty()
        //{
        //    var projectPath = Path.Combine(ProjectRootPath, "TestProject4");
        //    var subDirectory = Path.Combine(projectPath, "SubDir1");
        //    Directory.CreateDirectory(subDirectory);

        //    var exceptionWasThrown = false;
        //    try
        //    {

        //    }
           
        //}
    }
}